<?php

namespace App\Http\Controllers\Workflow;

use App\Enums\Workflow\WorkflowEnum;
use App\Http\Controllers\BaseController;
use App\Service\Workflow\WorkflowTemplateService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class WorkflowTemplateController extends BaseController
{
    private function commonValidate()
    {
        return [
            'name'                                      => 'required|string',
            'workflowType'                              => 'required|integer',
            'nodes'                                     => 'required|array',
            'nodes.*.nodeKey'                           => 'required',
            'nodes.*.name'                              => 'string',
            'nodes.*.businessType'                      => 'required|integer|' . Rule::in(array_keys(WorkflowEnum::BUSINESS_TYPE_TEXT)),
            'nodes.*.nodeType'                          => 'required|integer|' . Rule::in(array_keys(WorkflowEnum::NODE_TYPE_TEXT)),
            'nodes.*.cruiseTemplateId'                  => 'integer',
            'nodes.*.handlerType'                       => 'required|integer|' . Rule::in(array_keys(WorkflowEnum::HANDLER_TYPE_TEXT)),
            'nodes.*.handlerUid'                        => 'integer',
            'nodes.*.children'                          => 'array',
            'nodes.*.children.*.businessType'           => 'required|integer|' . Rule::in(array_keys(WorkflowEnum::BUSINESS_TYPE_TEXT)),
            'nodes.*.children.*.condition'              => 'required|array',
            'nodes.*.children.*.condition.nodeKey'      => 'required|integer',
            'nodes.*.children.*.condition.projectId'    => 'required|integer',
            'nodes.*.children.*.condition.formula'      => 'required|string|' . Rule::in(array_keys(WorkflowEnum::FORMULA_TEXT)),
            'nodes.*.children.*.condition.optionsValue' => 'required',
        ];
    }

    // 新增
    public function add(Request $request)
    {
        $this->validate($request, $this->commonValidate());

        $workflowTemplateService = new WorkflowTemplateService(getRmsUserInfoByAuth()->id, getRmsUserInfoByAuth()->username);

        return $this->success([
            'id' => $workflowTemplateService->addOrEdit(0, $request->name, $request->workflowType, $request->nodes)
        ]);
    }

    // 编辑
    public function edit(Request $request)
    {
        $this->validate($request, array_merge(['id' => 'required|integer'], $this->commonValidate()));

        $workflowTemplateService = new WorkflowTemplateService(getRmsUserInfoByAuth()->id, getRmsUserInfoByAuth()->username);
        $workflowTemplateService->addOrEdit($request->id, $request->name, $request->workflowType, $request->nodes);

        return $this->success();
    }

    // 列表
    public function index(Request $request)
    {
        $this->validate($request, [
            'pageSize' => 'integer',
            'pageNum'  => 'integer',
            'status'   => 'integer'
        ]);

        $pageSize     = (int)$request->get('pageSize', 15);
        $pageNum      = (int)$request->get('pageNum', 1);
        $status       = (int)$request->get('status', -1);
        $workflowType = (int)$request->get('workflowType');

        $workflowTemplateService = new WorkflowTemplateService(getRmsUserInfoByAuth()->id, getRmsUserInfoByAuth()->username);

        return $this->success($workflowTemplateService->index($pageNum, $pageSize, $status, $workflowType));
    }

    // 详情
    public function detail(Request $request, int $id)
    {
        $workflowTemplateService = new WorkflowTemplateService(getRmsUserInfoByAuth()->id, getRmsUserInfoByAuth()->username);
        return $this->success($workflowTemplateService->detail($id));
    }

    // 删除
    public function delete(Request $request, int $id)
    {
        $workflowTemplateService = new WorkflowTemplateService(getRmsUserInfoByAuth()->id, getRmsUserInfoByAuth()->username);
        $workflowTemplateService->delete($id);

        return $this->success();
    }

    /**
     * 上下架
     */
    public function onOff(Request $request, int $id)
    {
        $this->validate($request, [
            'status' => 'required|integer|in:0,1',
        ]);

        $workflowTemplateService = new WorkflowTemplateService(getRmsUserInfoByAuth()->id, getRmsUserInfoByAuth()->username);
        $workflowTemplateService->onOff($id, $request->status);

        return $this->success();
    }

    /**
     * 枚举
     * @param Request $request
     */
    public function enums(Request $request)
    {
        return $this->success(WorkflowEnum::getEnums());
    }
}
